package com.example.tianxiaoyu.startproject_128txy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.tianxiaoyu.startproject_128txy.bean.WeatherInfo;
import com.example.tianxiaoyu.startproject_128txy.listener.WeatherListener;
import com.example.tianxiaoyu.startproject_128txy.model.Weather51Model;
import com.example.tianxiaoyu.startproject_128txy.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements WeatherListener,View.OnClickListener {
    private EditText etcityno;
    private Button btnfind1, btnfind2;
    private TextView tvcityname;
    private TextView tvcitytemp;
    private TextView tvcityWD;
    private TextView tvcityWS;
    private TextView tvcitySD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//调用初始化方法，实例化控件
    }

    private void init() {
        etcityno = (EditText) findViewById(R.id.editText);
        btnfind1 = (Button) findViewById(R.id.button);
        btnfind2 = (Button) findViewById(R.id.button2);
        tvcityname = (TextView) findViewById(R.id.textView);
        tvcitytemp = (TextView) findViewById(R.id.textView4);
        tvcityWD = (TextView) findViewById(R.id.textView6);
        tvcityWS = (TextView) findViewById(R.id.textView8);
        tvcitySD = (TextView) findViewById(R.id.textView10);

        btnfind1.setOnClickListener(this);
        btnfind2.setOnClickListener(this);
    }

    @Override
    public void onResponse(WeatherInfo weatherInfo) {
        if (weatherInfo != null)
        {
            tvcityname.setText(weatherInfo.getCity());
            tvcitytemp.setText(weatherInfo.getTemp());
            tvcityWD.setText(weatherInfo.getWD());
            tvcityWS.setText(weatherInfo.getWS());
            tvcitySD.setText(weatherInfo.getSD());
        } else
        {
            tvcityname.setText("未知");
        tvcitytemp.setText("未知");
        tvcityWD.setText("未知");
        tvcityWS.setText("未知");
        tvcitySD.setText("未知");
    }

}

    @Override
    public void onFail(String msg) {
        tvcityname.setText(msg);
        tvcitytemp.setText(msg);
        tvcityWD.setText(msg);
        tvcityWS.setText(msg);
        tvcitySD.setText(msg);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button://查询1
                System.out.println("----按钮一被按下");
                WeatherModel weatherModel=new WeatherModel();
                weatherModel.getWeather(etcityno.getText().toString(),this);
                break;
            case R.id.button2://查询2
                System.out.println("----按钮二被按下");
                Weather51Model weather51Model=new Weather51Model();
                weather51Model.getWeather(etcityno.getText().toString(),this);
                break;
            default:
                break;
        }

    }

}
